<template>
    <el-container style="height: 98vh; border: 1px solid #eee;background: #fff">
        <SideBar />
        <el-container>
            <el-header style="width: 95%;margin-top: 10px;">
                <HeadBar />
            </el-header>
            <el-main id="main">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="新密码" prop="newPass">
                        <el-input type="password" v-model="ruleForm.newPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="checkNewPass">
                        <el-input type="password" v-model="ruleForm.checkNewPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="验证旧密码" prop="oldPass">
                        <el-input v-model.number="ruleForm.oldPass"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
import SideBar from '@/components/SideBar.vue'
import HeadBar from '@/components/HeadBar.vue'
import request from '@/utils/request';

export default {
    components: { SideBar, HeadBar },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validateNewPass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.newPass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        var validateOldPass = (rule, value, callback) => {
            const studentId = sessionStorage.getItem("studentId");
            request.get('/student/findByID?studentId=' + studentId).then(res => {
                console.log('res', res.data.data.password, value);
                if (res.data.data.password != value) {
                    callback(new Error('旧密码错误!'));
                } else {
                    callback();
                }
            }).catch(err => {
                console.error(err);
                callback(new Error('验证旧密码失败!'));
            });
        }
        return {
            ruleForm: {
                newPass: '',
                checkNewPass: '',
                oldPass: ''
            },
            rules: {
                newPass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkNewPass: [
                    { validator: validateNewPass, trigger: 'blur' }
                ],
                oldPass: [
                    { validator: validateOldPass, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    const studentId = sessionStorage.getItem("studentId");
                    request.get('/student/changePassword?studentId=' + studentId + '&oldPassword=' + this.ruleForm.oldPass + '&newPassword=' + this.ruleForm.newPass).then(res => {
                        if (res.data.state == '0') {
                            this.$message({
                                type: "success",
                                message: "修改成功"
                            });
                        } else {
                            this.$message({
                                type: "error",
                                message: "修改失败"
                            });
                        }
                    }).catch(err => {
                        console.error(err);
                        this.$message({
                            type: "error",
                            message: "修改失败"
                        });
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "修改失败"
                    });
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}

</script>

<style scoped>
#main {
    width: 95%;
    margin-top: 30px;
    align-items: center;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin-left: 50px
}
</style>